<?php
/**
 * User: 自娱自乐自逍遥
 * Date: 14-10-15
 * Time: 下午3:12
 */

namespace Admin\Logic;


use Think\Model;

class ModuleLogic extends Model{


    public function loadModuleConfig($module = ''){
        if(empty($module))$module = MODULE_NAME;
        $path = APP_PATH.$module.DIR_SEP.'module.inc.php';
        $config = null;
        if(file_exists($path)){
            $config = load_config($path);
            if(empty($config)){
                //$config['icon'] = __APP__.'/';
            }else{
                if(!is_url($config['icon']) || $config['icon']{0} != '/'){
                    $config['icon'] = __APP__.'/'.str_replace(array(ROOT_PATH,DIR_SEP),'',APP_PATH).'/'.$module.'/'.$config['icon'];
                }
            }
            return array(
                'path'  => APP_PATH.$module.DIR_SEP,
                'config'=> $config
            );
        }
        return array();
    }

    public function loadModuleList(){
        $dir = opendir(APP_PATH);
        $system_module = array('Admin','Common','Home','Member');
        $installd_modules = C('MODULE_ALLOW_LIST');
        $modules = array();
        while($file = readdir($dir)){
            if(in_array($file, $system_module) || $file == '.' || $file == '..')continue;
            if(file_exists(APP_PATH.$file.DIR_SEP.'module.inc.php')){
                $modules[$file] = $this->loadModuleConfig($file);
                $modules[$file]['installd'] = !!in_array($file,$installd_modules);
            }
        }
        closedir($dir);
        return $modules;
    }

    public function installModule($module_name = ''){
        $config = $this->loadModuleConfig($module_name);
        if(empty($config['config'])){
            return false;
        }else{
            $path = $config['path'];
            if(is_dir($path.'install') && is_file($path.'install'.DIR_SEP.'install.php')){
                include $path.'install'.DIR_SEP.'install.php';

            }
        }
    }
} 